//
//  C_SongerViewController.m
//  音悦咖
//
//  Created by Best on 15/7/15.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "C_SongerViewController.h"
#import "C_MusicListPlayerViewController.h"

@interface C_SongerViewController ()<UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>


@property (nonatomic, retain) NSMutableArray *buttonArr;
@property (nonatomic, retain) NSMutableArray *listArr;
@property (nonatomic, retain) NSMutableArray *titleArr;

@end

@implementation C_SongerViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.rightButton.hidden = YES;
   // self.view.backgroundColor = RGBA(30, 40, 60, 1);
    self.titleLabel.text = @"歌手";
    backgroundImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT - 64 - 49)];
    backgroundImage.image = [UIImage imageWithContentsOfFile:GETBUNDLE_PATH(@"blackgound", @"jpg")];
    [self.view addSubview:backgroundImage];
    backgroundImage.userInteractionEnabled = YES;
    [backgroundImage release];
    [self getdata];
    
    [self.leftButton addTarget:self action:@selector(buttonBlack) forControlEvents:(UIControlEventTouchUpInside)];
    
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    
}

#pragma mark Button返回方法
- (void)buttonBlack
{
    [self.navigationController popViewControllerAnimated:YES];
}


#pragma mark getdata
- (void)getdata
{
    
      [AFNetworkHandler GETWithAFNByURL:self.songer_url completion:^(id result) {
          self.buttonArr = [SongerModel arrayWithModelByArray:[result objectForKey:@"data"]];
         // NSLog(@"%@", [self.buttonArr[1] songId] );
          [self createButtonCollection];

          [self createListCollection];
          [self createTitleCollection];
      }];
}

- (void)getListARRDate:(NSString *)singer_Id
{

    NSString *str = [NSString stringWithFormat:@"%@%@%@",API_SONGER_URL_FOUND,singer_Id,API_SONGER_URL_BACK];
     [AFNetworkHandler GETWithAFNByURL:str completion:^(id result) {
         self.listArr = [C_SongerListModel arrayWithModelByArray:[result objectForKey:@"data"]];

         [songerListCollection reloadData];
     }];
    
}



#pragma mark createButtonCollection
- (void)createButtonCollection
{
    buttonlayout = [[[UICollectionViewFlowLayout alloc] init] autorelease];
    
    songerButtonCollection = [[UICollectionView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH - 100, 64, 90, backgroundImage.frame.size.height - 64 - 34) collectionViewLayout:buttonlayout];
    
    buttonlayout.itemSize = CGSizeMake(songerButtonCollection.frame.size.width, 30);
    songerButtonCollection.backgroundColor = [UIColor clearColor];
    songerButtonCollection.delegate = self;
    songerButtonCollection.dataSource = self;
    songerButtonCollection.tag = 2000;
    buttonlayout.minimumLineSpacing = 30;
    buttonlayout.collectionView.contentOffset = CGPointMake(0, buttonlayout.itemSize.height * buttonlayout.collectionView.numberOfSections / 2);
    [backgroundImage addSubview:songerButtonCollection];
    [songerButtonCollection release];
    [songerButtonCollection registerClass:[C_SongerCollectionViewCell class] forCellWithReuseIdentifier:@"C_SongerCollectionViewCell"];
    [songerButtonCollection registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"placeheader"];
    
}


#pragma mark createSongListCollection

- (void)createListCollection
{
    listLayout = [[[C_SongLsit_UICollectionViewLayout alloc] init] autorelease];
    songerListCollection = [[UICollectionView alloc] initWithFrame:CGRectMake(0, songerButtonCollection.frame.origin.y - 10, SCREEN_WIDTH - 110, songerButtonCollection.frame.size.height + 10) collectionViewLayout:listLayout];
    listLayout.itemSize = CGSizeMake(songerListCollection.frame.size.width - 80, 130);
    
    songerListCollection.tag = 2001;
    songerListCollection.delegate = self;
    songerListCollection.dataSource = self;
    songerListCollection.backgroundColor = [UIColor clearColor];
    [backgroundImage addSubview:songerListCollection];
    [songerListCollection release];
    //songerListCollection.backgroundColor = [UIColor redColor];
    [songerListCollection registerClass:[C_SongerListCollectionViewCell class] forCellWithReuseIdentifier:@"C_SongerListCollectionViewCell"];
    
    
}


#pragma mark creatTitleCollection

- (void)createTitleCollection
{
    titleLayout = [[[UICollectionViewFlowLayout alloc] init] autorelease];
    
    titleCollection = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 60) collectionViewLayout:titleLayout];
    titleCollection.delegate = self;
    titleCollection.dataSource = self;
    titleCollection.tag = 2002;
    titleCollection.backgroundColor = [UIColor clearColor];
    [backgroundImage addSubview:titleCollection];
    [titleCollection release];
    titleCollection.hidden = YES;
    titleLayout.itemSize = CGSizeMake(20, 20);
    titleLayout.minimumLineSpacing = 0;
    titleLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    titleLayout.sectionInset = UIEdgeInsetsMake(5, 0, 25, 0);
    [titleCollection registerClass:[C_SongerCollectionViewCell class] forCellWithReuseIdentifier:@"titleCell"];
    [titleCollection registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"titleHeader"];
    
}
#pragma mark 自动 轮播
- (void)changeconter
{     static int n = 0;
    n++;
    titleCollection.contentOffset = CGPointMake(40 * n, 0);
}



- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    switch (collectionView.tag) {
        case 2000: {
            
            UICollectionReusableView *reusable = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"placeheader" forIndexPath:indexPath];
            return reusable;
            break;
        }
        case 2002: {
            
            UICollectionReusableView *reusable = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"titleHeader" forIndexPath:indexPath];
            return reusable;
            break;
        }
        default:
            break;
    }
    return nil;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    switch (collectionView.tag) {
        case 2000:
            return CGSizeMake(songerButtonCollection.frame.size.width, 30);
            break;
        case 2002:
            return CGSizeMake(SCREEN_WIDTH / 2, titleCollection.frame.size.height);
            break;
        default:
            break;
    }
    return CGSizeMake(0, 0);
}


#pragma mark CollectionDelegate
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    switch (collectionView.tag) {
        case 2000:
            return 1000;
            break;
        case 2002:
            return 100000;
            break;
        default:
            break;
    }
    return 1;
}



- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    switch (collectionView.tag) {
        case 2000: {
            return self.buttonArr.count;
            break;
        }
        case 2001: {
            if (!self.listArr) {
                return 0;
            } else {
                return self.listArr.count;
            }
            break;
        }
        case 2002: {
            return self.titleArr.count;
            break;
        }
        default:
            break;
    }
    return 0;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    switch (collectionView.tag) {
        case 2000: {
            C_SongerCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"C_SongerCollectionViewCell" forIndexPath:indexPath];
            cell.model = self.buttonArr[indexPath.item];
 
            return cell;
            
            break;
        }
        case 2001: {
            if (!self.listArr) {
                C_SongerListCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"C_SongerListCollectionViewCell" forIndexPath:indexPath];
                cell.imageView.image = [UIImage imageWithContentsOfFile:GETBUNDLE_PATH(@"place", @"jpg")];
              //  cell.layer.cornerRadius = cell.frame.size.height / 2;

                return cell;
            } else {
                C_SongerListCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"C_SongerListCollectionViewCell" forIndexPath:indexPath];
                cell.model = self.listArr[indexPath.item];
                [cell.nameLabel sizeToFit];
                //cell.layer.cornerRadius = cell.frame.size.height / 2;
                return cell;
            }
        case 2002: {
            C_SongerCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"titleCell" forIndexPath:indexPath];
            cell.buttonLabel.text = self.titleArr[indexPath.item];
            cell.buttonLabel.font = [UIFont systemFontOfSize:18];
            cell.buttonLabel.textColor = COLOR_STOCHASTIC;
            return cell;
            break;
        }
            
            break;
        }
        default:
            break;
    }
    return nil;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    switch (collectionView.tag) {
        case 2000: {
//            if (timer) {
//                [timer setFireDate:[NSDate distantFuture]];
//            }
            [self getListARRDate:[self.buttonArr[indexPath.item] songId]];
           // SongerModel *model = self.buttonArr[indexPath.item];
//         //   NSLog(@"%d", [model.details length]);
//            if ([model.details isEqualToString:@""]) {
//              //  NSLog(@"nil");
//                model.details = @"音乐咖量身为您打造最舒适的音乐";
//          //      NSLog(@"新%d", [model.details length]);
//            }
//            
//            self.titleArr = [NSMutableArray array];
//            
//            for (NSInteger i = 0; i < [model.details length]; i++) {
//                
//                [self.titleArr addObject:[model.details substringWithRange:NSMakeRange(i, 1)]];
//            }
//         //   NSLog(@"%d", self.titleArr.count);
//            [titleCollection reloadData];
//            titleCollection.hidden = NO;
//            if (!timer) {
//                timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(changeconter) userInfo:(nil) repeats:YES];
//            } else {
//                [timer setFireDate:[NSDate distantPast]];
//                
//            }
            break;
        }
        case 2001: {
            C_MusicListPlayerViewController *list = [[C_MusicListPlayerViewController alloc] init];
            list.baseId = [self.listArr[indexPath.item] singer_id];
          //  NSLog(@"%@", self.listArr);
//            [self presentViewController:list animated:YES completion:^{
//                
//                
//            }];
            [self.navigationController pushViewController:list animated:YES];
            
            break;
        }
        
        
        default:
            break;
    }
}
- (void)viewDidDisappear:(BOOL)animated
{
    
    [timer invalidate];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
